home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / test / test_grammar.py < prev    next >
Encoding:
Python Source  |  2000-03-29  |  11.2 KB  |  545 lines

  1. # Python test set -- part 1, grammar.
  2. # This just tests whether the parser accepts them all.
  3.  
  4. from test_support import *
  5.  
  6. print '1. Parser'
  7.  
  8. print '1.1 Tokens'
  9.  
  10. print '1.1.1 Backslashes'
  11.  
  12. # Backslash means line continuation:
  13. x = 1 \
  14. + 1
  15. if x <> 2: raise TestFailed, 'backslash for line continuation'
  16.  
  17. # Backslash does not means continuation in comments :\
  18. x = 0
  19. if x <> 0: raise TestFailed, 'backslash ending comment'
  20.  
  21. print '1.1.2 Numeric literals'
  22.  
  23. print '1.1.2.1 Plain integers'
  24. if 0xff <> 255: raise TestFailed, 'hex int'
  25. if 0377 <> 255: raise TestFailed, 'octal int'
  26. if  2147483647   != 017777777777: raise TestFailed, 'large positive int'
  27. try:
  28.     from sys import maxint
  29. except ImportError:
  30.     maxint = 2147483647
  31. if maxint == 2147483647:
  32.     if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
  33.     # XXX -2147483648
  34.     if 037777777777 != -1: raise TestFailed, 'oct -1'
  35.     if 0xffffffff != -1: raise TestFailed, 'hex -1'
  36.     for s in '2147483648', '040000000000', '0x100000000':
  37.         try:
  38.             x = eval(s)
  39.         except OverflowError:
  40.             continue
  41. ##        raise TestFailed, \
  42.         print \
  43.               'No OverflowError on huge integer literal ' + `s`
  44. elif eval('maxint == 9223372036854775807'):
  45.     if eval('-9223372036854775807-1 != 01000000000000000000000'):
  46.         raise TestFailed, 'max negative int'
  47.     if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
  48.     if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
  49.     for s in '9223372036854775808', '02000000000000000000000', \
  50.          '0x10000000000000000':
  51.         try:
  52.             x = eval(s)
  53.         except OverflowError:
  54.             continue
  55.         raise TestFailed, \
  56.               'No OverflowError on huge integer literal ' + `s`
  57. else:
  58.     print 'Weird maxint value', maxint
  59.  
  60. print '1.1.2.2 Long integers'
  61. x = 0L
  62. x = 0l
  63. x = 0xffffffffffffffffL
  64. x = 0xffffffffffffffffl
  65. x = 077777777777777777L
  66. x = 077777777777777777l
  67. x = 123456789012345678901234567890L
  68. x = 123456789012345678901234567890l
  69.  
  70. print '1.1.2.3 Floating point'
  71. x = 3.14
  72. x = 314.
  73. x = 0.314
  74. # XXX x = 000.314
  75. x = .314
  76. x = 3e14
  77. x = 3E14
  78. x = 3e-14
  79. x = 3e+14
  80. x = 3.e14
  81. x = .3e14
  82. x = 3.1e4
  83.  
  84. print '1.1.3 String literals'
  85.  
  86. ##def assert(s):
  87. ##    if not s: raise TestFailed, 'see traceback'
  88.  
  89. x = ''; y = ""; assert(len(x) == 0 and x == y)
  90. x = '\''; y = "'"; assert(len(x) == 1 and x == y and ord(x) == 39)
  91. x = '"'; y = "\""; assert(len(x) == 1 and x == y and ord(x) == 34)
  92. x = "doesn't \"shrink\" does it"
  93. y = 'doesn\'t "shrink" does it'
  94. assert(len(x) == 24 and x == y)
  95. x = "does \"shrink\" doesn't it"
  96. y = 'does "shrink" doesn\'t it'
  97. assert(len(x) == 24 and x == y)
  98. x = """
  99. The "quick"
  100. brown fox
  101. jumps over
  102. the 'lazy' dog.
  103. """
  104. y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
  105. assert(x == y)
  106. y = '''
  107. The "quick"
  108. brown fox
  109. jumps over
  110. the 'lazy' dog.
  111. '''; assert(x == y)
  112. y = "\n\
  113. The \"quick\"\n\
  114. brown fox\n\
  115. jumps over\n\
  116. the 'lazy' dog.\n\
  117. "; assert(x == y)
  118. y = '\n\
  119. The \"quick\"\n\
  120. brown fox\n\
  121. jumps over\n\
  122. the \'lazy\' dog.\n\
  123. '; assert(x == y)
  124.  
  125.  
  126. print '1.2 Grammar'
  127.  
  128. print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
  129. # XXX can't test in a script -- this rule is only used when interactive
  130.  
  131. print 'file_input' # (NEWLINE | stmt)* ENDMARKER
  132. # Being tested as this very moment this very module
  133.  
  134. print 'expr_input' # testlist NEWLINE
  135. # XXX Hard to test -- used only in calls to input()
  136.  
  137. print 'eval_input' # testlist ENDMARKER
  138. x = eval('1, 0 or 1')
  139.  
  140. print 'funcdef'
  141. ### 'def' NAME parameters ':' suite
  142. ### parameters: '(' [varargslist] ')'
  143. ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
  144. ###            | ('**'|'*' '*') NAME)
  145. ###            | fpdef ['=' test] (',' fpdef ['=' test])* [',']  
  146. ### fpdef: NAME | '(' fplist ')'
  147. ### fplist: fpdef (',' fpdef)* [',']
  148. ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
  149. ### argument: [test '='] test    # Really [keyword '='] test
  150. def f1(): pass
  151. f1()
  152. f1(*())
  153. f1(*(), **{})
  154. def f2(one_argument): pass
  155. def f3(two, arguments): pass
  156. def f4(two, (compound, (argument, list))): pass
  157. def a1(one_arg,): pass
  158. def a2(two, args,): pass
  159. def v0(*rest): pass
  160. def v1(a, *rest): pass
  161. def v2(a, b, *rest): pass
  162. def v3(a, (b, c), *rest): pass
  163. def d01(a=1): pass
  164. d01()
  165. d01(1)
  166. d01(*(1,))
  167. d01(**{'a':2})
  168. def d11(a, b=1): pass
  169. d11(1)
  170. d11(1, 2)
  171. d11(1, **{'b':2})
  172. def d21(a, b, c=1): pass
  173. d21(1, 2)
  174. d21(1, 2, 3)
  175. d21(*(1, 2, 3))
  176. d21(1, *(2, 3))
  177. d21(1, 2, *(3,))
  178. d21(1, 2, **{'c':3})
  179. def d02(a=1, b=2): pass
  180. d02()
  181. d02(1)
  182. d02(1, 2)
  183. d02(*(1, 2))
  184. d02(1, *(2,))
  185. d02(1, **{'b':2})
  186. d02(**{'a': 1, 'b': 2})
  187. def d12(a, b=1, c=2): pass
  188. d12(1)
  189. d12(1, 2)
  190. d12(1, 2, 3)
  191. def d22(a, b, c=1, d=2): pass
  192. d22(1, 2)
  193. d22(1, 2, 3)
  194. d22(1, 2, 3, 4)
  195. def d01v(a=1, *rest): pass
  196. d01v()
  197. d01v(1)
  198. d01v(1, 2)
  199. d01v(*(1, 2, 3, 4))
  200. d01v(*(1,))
  201. d01v(**{'a':2})
  202. def d11v(a, b=1, *rest): pass
  203. d11v(1)
  204. d11v(1, 2)
  205. d11v(1, 2, 3)
  206. def d21v(a, b, c=1, *rest): pass
  207. d21v(1, 2)
  208. d21v(1, 2, 3)
  209. d21v(1, 2, 3, 4)
  210. d21v(*(1, 2, 3, 4))
  211. d21v(1, 2, **{'c': 3})
  212. def d02v(a=1, b=2, *rest): pass
  213. d02v()
  214. d02v(1)
  215. d02v(1, 2)
  216. d02v(1, 2, 3)
  217. d02v(1, *(2, 3, 4))
  218. d02v(**{'a': 1, 'b': 2})
  219. def d12v(a, b=1, c=2, *rest): pass
  220. d12v(1)
  221. d12v(1, 2)
  222. d12v(1, 2, 3)
  223. d12v(1, 2, 3, 4)
  224. d12v(*(1, 2, 3, 4))
  225. d12v(1, 2, *(3, 4, 5))
  226. d12v(1, *(2,), **{'c': 3})
  227. def d22v(a, b, c=1, d=2, *rest): pass
  228. d22v(1, 2)
  229. d22v(1, 2, 3)
  230. d22v(1, 2, 3, 4)
  231. d22v(1, 2, 3, 4, 5)
  232. d22v(*(1, 2, 3, 4))
  233. d22v(1, 2, *(3, 4, 5))
  234. d22v(1, *(2, 3), **{'d': 4})
  235.  
  236. ### stmt: simple_stmt | compound_stmt
  237. # Tested below
  238.  
  239. ### simple_stmt: small_stmt (';' small_stmt)* [';']
  240. print 'simple_stmt'
  241. x = 1; pass; del x
  242.  
  243. ### small_stmt: expr_stmt | print_stmt  | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
  244. # Tested below
  245.  
  246. print 'expr_stmt' # (exprlist '=')* exprlist
  247. 1
  248. 1, 2, 3
  249. x = 1
  250. x = 1, 2, 3
  251. x = y = z = 1, 2, 3
  252. x, y, z = 1, 2, 3
  253. abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
  254. # NB these variables are deleted below
  255.  
  256. print 'print_stmt' # 'print' (test ',')* [test]
  257. print 1, 2, 3
  258. print 1, 2, 3,
  259. print
  260. print 0 or 1, 0 or 1,
  261. print 0 or 1
  262.  
  263. print 'del_stmt' # 'del' exprlist
  264. del abc
  265. del x, y, (z, xyz)
  266.  
  267. print 'pass_stmt' # 'pass'
  268. pass
  269.  
  270. print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
  271. # Tested below
  272.  
  273. print 'break_stmt' # 'break'
  274. while 1: break
  275.  
  276. print 'continue_stmt' # 'continue'
  277. i = 1
  278. while i: i = 0; continue
  279.  
  280. print 'return_stmt' # 'return' [testlist]
  281. def g1(): return
  282. def g2(): return 1
  283. g1()
  284. x = g2()
  285.  
  286. print 'raise_stmt' # 'raise' test [',' test]
  287. try: raise RuntimeError, 'just testing'
  288. except RuntimeError: pass
  289. try: raise KeyboardInterrupt
  290. except KeyboardInterrupt: pass
  291.  
  292. print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
  293. import sys
  294. import time, sys
  295. from time import time
  296. from sys import *
  297. from sys import path, argv
  298.  
  299. print 'global_stmt' # 'global' NAME (',' NAME)*
  300. def f():
  301.     global a
  302.     global a, b
  303.     global one, two, three, four, five, six, seven, eight, nine, ten
  304.  
  305. print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
  306. def f():
  307.     z = None
  308.     del z
  309.     exec 'z=1+1\n'
  310.     if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n'
  311.     del z
  312.     exec 'z=1+1'
  313.     if z <> 2: raise TestFailed, 'exec \'z=1+1\''
  314. f()
  315. g = {}
  316. exec 'z = 1' in g
  317. if g.has_key('__builtins__'): del g['__builtins__']
  318. if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
  319. g = {}
  320. l = {}
  321. exec 'global a; a = 1; b = 2' in g, l
  322. if g.has_key('__builtins__'): del g['__builtins__']
  323. if l.has_key('__builtins__'): del l['__builtins__']
  324. if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'
  325.  
  326.  
  327. ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
  328. # Tested below
  329.  
  330. print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
  331. if 1: pass
  332. if 1: pass
  333. else: pass
  334. if 0: pass
  335. elif 0: pass
  336. if 0: pass
  337. elif 0: pass
  338. elif 0: pass
  339. elif 0: pass
  340. else: pass
  341.  
  342. print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
  343. while 0: pass
  344. while 0: pass
  345. else: pass
  346.  
  347. print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
  348. for i in 1, 2, 3: pass
  349. for i, j, k in (): pass
  350. else: pass
  351. class Squares:
  352.     def __init__(self, max):
  353.         self.max = max
  354.         self.sofar = []
  355.     def __len__(self): return len(self.sofar)
  356.     def __getitem__(self, i):
  357.         if not 0 <= i < self.max: raise IndexError
  358.         n = len(self.sofar)
  359.         while n <= i:
  360.             self.sofar.append(n*n)
  361.             n = n+1
  362.         return self.sofar[i]
  363. n = 0
  364. for x in Squares(10): n = n+x
  365. if n != 285: raise TestFailed, 'for over growing sequence'
  366.  
  367. print 'try_stmt'
  368. ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
  369. ###         | 'try' ':' suite 'finally' ':' suite
  370. ### except_clause: 'except' [expr [',' expr]]
  371. try:
  372.     1/0
  373. except ZeroDivisionError:
  374.     pass
  375. else:
  376.     pass
  377. try: 1/0
  378. except EOFError: pass
  379. except TypeError, msg: pass
  380. except RuntimeError, msg: pass
  381. except: pass
  382. else: pass
  383. try: 1/0
  384. except (EOFError, TypeError, ZeroDivisionError): pass
  385. try: 1/0
  386. except (EOFError, TypeError, ZeroDivisionError), msg: pass
  387. try: pass
  388. finally: pass
  389.  
  390. print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
  391. if 1: pass
  392. if 1:
  393.     pass
  394. if 1:
  395.     #
  396.     #
  397.     #
  398.     pass
  399.     pass
  400.     #
  401.     pass
  402.     #
  403.  
  404. print 'test'
  405. ### and_test ('or' and_test)*
  406. ### and_test: not_test ('and' not_test)*
  407. ### not_test: 'not' not_test | comparison
  408. if not 1: pass
  409. if 1 and 1: pass
  410. if 1 or 1: pass
  411. if not not not 1: pass
  412. if not 1 and 1 and 1: pass
  413. if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
  414.  
  415. print 'comparison'
  416. ### comparison: expr (comp_op expr)*
  417. ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
  418. if 1: pass
  419. x = (1 == 1)
  420. if 1 == 1: pass
  421. if 1 != 1: pass
  422. if 1 <> 1: pass
  423. if 1 < 1: pass
  424. if 1 > 1: pass
  425. if 1 <= 1: pass
  426. if 1 >= 1: pass
  427. if 1 is 1: pass
  428. if 1 is not 1: pass
  429. if 1 in (): pass
  430. if 1 not in (): pass
  431. if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
  432.  
  433. print 'binary mask ops'
  434. x = 1 & 1
  435. x = 1 ^ 1
  436. x = 1 | 1
  437.  
  438. print 'shift ops'
  439. x = 1 << 1
  440. x = 1 >> 1
  441. x = 1 << 1 >> 1
  442.  
  443. print 'additive ops'
  444. x = 1
  445. x = 1 + 1
  446. x = 1 - 1 - 1
  447. x = 1 - 1 + 1 - 1 + 1
  448.  
  449. print 'multiplicative ops'
  450. x = 1 * 1
  451. x = 1 / 1
  452. x = 1 % 1
  453. x = 1 / 1 * 1 % 1
  454.  
  455. print 'unary ops'
  456. x = +1
  457. x = -1
  458. x = ~1
  459. x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
  460. x = -1*1/1 + 1*1 - ---1*1
  461.  
  462. print 'selectors'
  463. ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
  464. ### subscript: expr | [expr] ':' [expr]
  465. f1()
  466. f2(1)
  467. f2(1,)
  468. f3(1, 2)
  469. f3(1, 2,)
  470. f4(1, (2, (3, 4)))
  471. v0()
  472. v0(1)
  473. v0(1,)
  474. v0(1,2)
  475. v0(1,2,3,4,5,6,7,8,9,0)
  476. v1(1)
  477. v1(1,)
  478. v1(1,2)
  479. v1(1,2,3)
  480. v1(1,2,3,4,5,6,7,8,9,0)
  481. v2(1,2)
  482. v2(1,2,3)
  483. v2(1,2,3,4)
  484. v2(1,2,3,4,5,6,7,8,9,0)
  485. v3(1,(2,3))
  486. v3(1,(2,3),4)
  487. v3(1,(2,3),4,5,6,7,8,9,0)
  488. print
  489. import sys, time
  490. c = sys.path[0]
  491. x = time.time()
  492. x = sys.modules['time'].time()
  493. a = '01234'
  494. c = a[0]
  495. c = a[-1]
  496. s = a[0:5]
  497. s = a[:5]
  498. s = a[0:]
  499. s = a[:]
  500. s = a[-5:]
  501. s = a[:-1]
  502. s = a[-4:-3]
  503.  
  504. print 'atoms'
  505. ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
  506. ### dictmaker: test ':' test (',' test ':' test)* [',']
  507.  
  508. x = (1)
  509. x = (1 or 2 or 3)
  510. x = (1 or 2 or 3, 2, 3)
  511.  
  512. x = []
  513. x = [1]
  514. x = [1 or 2 or 3]
  515. x = [1 or 2 or 3, 2, 3]
  516. x = []
  517.  
  518. x = {}
  519. x = {'one': 1}
  520. x = {'one': 1,}
  521. x = {'one' or 'two': 1 or 2}
  522. x = {'one': 1, 'two': 2}
  523. x = {'one': 1, 'two': 2,}
  524. x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
  525.  
  526. x = `x`
  527. x = `1 or 2 or 3`
  528. x = x
  529. x = 'x'
  530. x = 123
  531.  
  532. ### exprlist: expr (',' expr)* [',']
  533. ### testlist: test (',' test)* [',']
  534. # These have been exercised enough above
  535.  
  536. print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
  537. class B: pass
  538. class C1(B): pass
  539. class C2(B): pass
  540. class D(C1, C2, B): pass
  541. class C:
  542.     def meth1(self): pass
  543.     def meth2(self, arg): pass
  544.     def meth3(self, a1, a2): pass
  545.